
/**
 * 
 * @type {Object}
 * get  获取cookie的值
 * set  设置cookie
 *          name     cookie的名称
 *          value    cookie的值
 *          expires  有限日期  如果不设置这个东西，浏览器关闭之后此cookie就失效了
 *          path     cookie值保存的路径，默认与创建页路径一致。
 *          domain   cookie域名属性，默认与创建页域名一样
 *          secure   一个布尔值，表示传输cookie值时，是否需要一个安全协议
 */
import Config from './../../../config.js';
const u = navigator.userAgent;
export default {
    getCookie : function (name) {
        var cookieValue = "";
        var cookies = document.cookie.split('; ');
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            if (parts != null && parts != "undefined") {
                if (parts[0] === name) {
                    if (parts[1] != null && parts[1] != "undefined") {
                        cookieValue = parts[1];
                    }
                }
            }
        }
        return cookieValue;
    },
    setCookie : function (name, value, expires, path, domain, secure) {
        var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
        // if (expires instanceof Date) {
            cookieText += "; expires=" + new Date("July 21, 2300 01:15:00").toGMTString();
        // }

        if (path) {
            cookieText += "; path=" + path;
        }

        if (domain) {
            cookieText += "; domain=" + domain;
        }

        if (secure) {
            cookieText += "; secure";
        }
        document.cookie = cookieText;
    },
    clearAllCookie:function () {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if(keys) {
            for(var i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
        }
    },
    clearOneCookie:function(cookieName){
        var cookies = document.cookie.split(";");//将所有cookie键值对通过分号分割为数组

        //循环遍历所有cookie键值对
        for (var i = 0; i < cookies.length; i++) {
            //有些cookie键值对前面会莫名其妙产生一个空格，将空格去掉
            if (cookies[i].indexOf(" ") == 0) {
                cookies[i] = cookies[i].substring(1);
            }
    
            //比较每个cookie的名称，找到要删除的那个cookie键值对
            if (cookies[i].indexOf(cookieName) == 0) {
                var exp = new Date();//获取客户端本地当前系统时间
                exp.setTime(exp.getTime() - 60 * 1000);//将exp设置为客户端本地时间1分钟以前，将exp赋值给cookie作为过期时间后，就表示该cookie已经过期了, 那么浏览器就会将其立刻删除掉
    
                document.cookie = cookies[i] + ";expires=" + exp.toUTCString();//设置要删除的cookie的过期时间，即在该cookie的键值对后面再添加一个expires键值对，并将上面的exp赋给expires作为值(注意expires的值必须为UTC或者GMT时间，不能用本地时间），那么浏览器就会将该cookie立刻删除掉
                //注意document.cookie的用法很巧妙，在对其进行赋值的时候是设置单个cookie的信息，但是获取document.cookie的值的时候是返回所有cookie的信息
    
                break;//要删除的cookie已经在客户端被删除掉，跳出循环
            }
        }


    },
    /**
     * 删除cookie
     * cookie的名称
     */
    delCookie : function (name) {
        setCookie(name, ' ', -1);
    },
/**
 *  基础工具方法
    * isInteger                  判断非零整数 返回bool值
    * trim                      去掉字符串前后所有空格
    * getUrlParms               get获取url的value带解码
    * isWeiXin                  判断是否是微信环境   返回true 或 false
    * delectFace                过滤用户数据的表情符号
    * formatUrlForAjaxCall      给url添加时间戳，防止缓存
    * returnFloat               保留小数点后两位
*/
    isInteger: function (num){
        var regu = /^[1-9]\d*$/;
        return !regu.test(num)?false:true;
    },
    secondToDate:function(result) {
        var h = Math.floor(result / 3600);
        var m = Math.floor((result / 60 % 60));
        var s = Math.floor((result % 60));
        if(h == '0' && m != '0'){
            return result = m + ":" + s+"";
        }else if(h =='0'&& m == '0'){
            return result = s+"";
        }
        else if(h != '0'){
            return result = h + ":" +    m + ":" + s+"";
        }
        
    },
    timeToms:function(result){
        var m =0,
            s=0;
        m =parseInt(result/60);
        s= result % 60;
        return m + ':' + (s.toString().length==1? '0'+s : s);

    },
    isRealNum: function(val){  
        // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除    
        if(val === "" || val == null){        
            return false;    
        }    
        if(!isNaN(val)){
            return true;    
        }else{        
            return false;    
        }
    },         
    trim:function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    getUrlParms:function () {
        var args = new Object();
        var query = location.search.substring(1);//获取查询串
        var pairs = query.split("&");//在逗号处断开
        for (var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');//查找name=value
            if (pos == -1) continue;//如果没有找到就跳过
            var argname = pairs[i].substring(0, pos);//提取name
            var value = pairs[i].substring(pos + 1);//提取value
            // args[argname]=unescape(value);//存为属性
            args[argname] = decodeURIComponent(unescape(value));//存为属性
        }
        return args;
    },
    isWeiXin:function () {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
            return true;
        } else {
            return false;
        }
    },
    delectFace:function (str) {
        var result;
        result = str.replace(regStr = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, "");
        return result;
    },
    formatUrlForAjaxCall:function (url) {
        if (!url) {
            return url;
        }
        if (url.indexOf('?') > 0) {
            return url + '&d=' + new Date().getTime();
        } else {
            return url + '?d=' + new Date().getTime();
        }
    },
    returnFloat : function (value) {
        var value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split('.');
        if( xsd.length == 1 ){
            value = value.toString()+'.00';
            return value;
        }
        if( xsd.length >1){
            if(xsd[1].length <2){
                value = value.toString()+'0';
            }
            return value;
        }
    },
    //返回回放时间
    timestampToTime:function(timestamp) {
        var date =new Date(timestamp*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000  
        var timer = ''; 
        var Y = date.getFullYear();    
        var M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1);    
        var D = date.getDate();    
        var h = (date.getHours() <10 ?'0' + (date.getHours()) : date.getHours());    
        var m = (date.getMinutes() <10 ?'0' + (date.getMinutes()) : date.getMinutes());    
        var s = (date.getSeconds() <10 ?'0' + (date.getSeconds()) : date.getSeconds());
        var data1 =  new Date(Date.parse(new Date()));
        var M1 = (data1.getMonth() +1 <10 ?'0' + (data1.getMonth() +1) : data1.getMonth() +1);    
        var D1 = data1.getDate(); 
        if(M1 == M && D1 == D){
            timer =h+':'+m;
        }else{
            timer = M+'月'+D+'日'+' ' +h+':'+m;
        }
        return timer;
    },
    timestampToTime1:function(timestamp) {
        var date =new Date(timestamp*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000  
        var timer = ''; 
        var Y = date.getFullYear();    
        var M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1);    
        var D = date.getDate();    
        var h = date.getHours() ;    
        var m = date.getMinutes();    
        var s = date.getSeconds();
        var data1 =  new Date(Date.parse(new Date()));
        var M1 = (data1.getMonth() +1 <10 ?'0' + (data1.getMonth() +1) : data1.getMonth() +1);    
        var D1 = data1.getDate(); 
        if(M1 == M && D1 == D){
            timer ='';
        }else{
            timer = M+'月'+D+'日';
        }
        
        return timer;
    },
    gettime:function(){
        var startAend;
        var curDate = new Date();
        var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天 
        var y = preDate.getFullYear();    //获取完整的年份(4位,1970-????)
        var m =preDate.getMonth();       //获取当前月份(0-11,0代表1月)
        var d = preDate.getDate();        //获取当前日(1-31)  
        var a = (m+1)
        if(a.length == '1'){
            startAend = y+'-0'+a+'-'+d;
        }else{
            startAend = y+'-0'+a+'-'+d;
        }
        return startAend;
    },
    https:function(){
       return Config.apiUrl;
    },
    https1:function(){
        return Config.apiUrl1;
    },
    imgUrl:function(){
        return Config.imgUrl;
    },
    imgUrlImg:function(){
        return Config.imgUrlImg;
    },
    imgUrlIds:function(){
        return Config.imgUrlIds;
    },
    imgUrlVideos:function(){
        return Config.imgUrlVideo;
    },
    RongId:function(){
        return Config.rongId;
    },
    DsApiUrl:function(){
        return Config.dsApiUrl;
    },
    DsAppId:function(){
        return Config.dsAppId;
    },
    loginPath:function(){
        return Config.pagePath;
    },
    httpsImg:function(){
        return Config.imgApiUrl;
    },
    formatDate:function (date, fmt) {
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, 
                    (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        };
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                let str = o[k] + '';
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? 
                str : ('00' + str).substr(str.length));
            }
        }
        return fmt;
    },
    code:{
        successRes:'200',
        //超时
        noLogin:'481',
        raleNoLogin:'483',
        //业务逻辑错误
        serviceError:'484',
    },
    H5key:'O2018@(*cbctcbct*)@8102O',
    imgSize:'2048000',
    toast: function (text) {
        var bodyDiv = document.getElementsByTagName('body')[0];
        // var child = document.getElementById('collectPop');
        // if(child){
        //     bodyDiv.removeChild(child);
        // }
        
        var html = '<div id="collectPop">' +
            '<div class="cOll_pop">' + text + '</div>' +
            '</div>';
        bodyDiv.innerHTML+= html;
        var childs = document.getElementById('collectPop');
        // $('.cOll_pop').css('top', '40%');
        // $("#collectPop").show();
        setTimeout(function () {
            bodyDiv.removeChild(childs);
        }, 1000);
    },
    androidDownloadAddress:Config.androidDownloadAddress,
    iosDownloadAddress:Config.iosDownloadAddress,
    /*
     *下载相关处理 
     */
    isAndroid: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
    isIOS: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
    // 安卓下载地址
    downLoadAndroid:function(){
        var ua = navigator.userAgent.split("(")[1].split(")")[0];
        var phone = [/IPHONE/gi, /huawei/gi, /mi/gi, /vivo/gi, /OPPO/gi, /samsung/gi, /SONY/gi, /Nokia/gi, /HTC/gi, /ZTE/gi, /Lenovo/gi, /ZUK/gi,]
        if (phone[1].test(ua)) {
            window.location.href='https://appgallery.cloud.huawei.com/uowap/index.html#/detailApp/C100594819?source=appshare&subsource=C100594819';
        } else if (phone[2].test(ua)) {
            window.location.href='http://app.xiaomi.com/detail/760715';
        } else if (phone[3].test(ua)) {
            window.location.href='http://appdetailh5.vivo.com.cn/detail/2418329?source=7';
        } else if (phone[4].test(ua)) {
            window.location.href='http://appdetailh5.vivo.com.cn/detail/2418329?source=7';
        } else {
            window.location.href = 'http://img001.cbct.cn/package/SFM_M_A_S_cbct.apk';
        }
    },
    //  ios下载地址
    downLoadIos:function(){
        window.location = "https://itunes.apple.com/us/app/id1439524045?ls=1&mt=8";
    },
    
}
